<form class="default">
	<fieldset>
		<legend>Фильтр</legend>
		<div class="row">
			<div class="label">Раздел</div>
			<div class="field">
				<select id="topics_filter_section" style="width: 200px;"><option value=""></option><?php foreach ($sections as $section): echo '<option value="'.$section->id.'">'.$section->name.'</option>'; endforeach; ?></select>
				<script>
					$('#topics_filter_section').change(function() {
						var location = '<?php echo URL::site(Route::get('backend')->uri(array(
							'controller' => 'forum',
							'action' => 'messages',
							'par1' => 'section',
							'par2' => '{par2}'
						))); ?>';
						location = location.replace('{par2}', $(this).val());
						window.location.href = location;
						return false;
					});
					<?php if (Request::current()->param('par1') == 'section' AND Valid::digit(Request::current()->param('par2'))): ?>$('#topics_filter_section').val('<?php echo Request::current()->param('par2'); ?>');<?php endif; ?>
				</script>
			</div>
			<span style="margin: 0 14px;">или</span>
			<div class="label">Категория</div>
			<div class="field">
				<select id="topics_filter_cat" style="width: 200px;"><option value=""></option><?php foreach ($cats as $cat): echo '<option value="'.$cat->id.'">'.$cat->name.'</option>'; endforeach; ?></select>
				<script>
					$('#topics_filter_cat').change(function() {
						var location = '<?php echo URL::site(Route::get('backend')->uri(array(
							'controller' => 'forum',
							'action' => 'messages',
							'par1' => 'cat',
							'par2' => '{par2}'
						))); ?>';
						location = location.replace('{par2}', $(this).val());
						window.location.href = location;
						return false;
					});
					<?php if (Request::current()->param('par1') == 'cat' AND Valid::digit(Request::current()->param('par2'))): ?>$('#topics_filter_cat').val('<?php echo Request::current()->param('par2'); ?>');<?php endif; ?>
				</script>
			</div>
			<span style="margin: 0 14px;">или</span>
		</div>
		<div class="row">
			<div class="label">Тема</div>
			<div class="field">
				<select id="messages_filter_topic" style="width: 200px;"><option value=""></option><?php foreach ($topics as $topic): echo '<option value="'.$topic->id.'">'.$topic->name.'</option>'; endforeach; ?></select>
				<script>
					$('#messages_filter_topic').change(function() {
						var location = '<?php echo URL::site(Route::get('backend')->uri(array(
							'controller' => 'forum',
							'action' => 'messages',
							'par1' => 'topic',
							'par2' => '{par2}'
						))); ?>';
						location = location.replace('{par2}', $(this).val());
						window.location.href = location;
						return false;
					});
					<?php if (Request::current()->param('par1') == 'topic' AND Valid::digit(Request::current()->param('par2'))): ?>$('#messages_filter_topic').val('<?php echo Request::current()->param('par2'); ?>');<?php endif; ?>
				</script>
			</div> 
		</div>
	</fieldset>
</form>

<?php echo $pagination; ?>

<table class="default">
	<thead>
		<tr>
			<?php foreach ($columns as $column => $data): ?>
				<td><?php echo $column; ?></td>
			<?php endforeach; ?>
			<td></td><td></td>
		</tr>
	</thead>
	<tbody>
		<?php foreach ($messages as $message): ?>
		<tr>
			<?php foreach ($columns as $column => $data): ?>
				<td><?php 
					if ($column == 'user_id')
						echo $message->{$column}.': '.Model_User::nickname($message->user);
					elseif ($column == 'section_id')
						echo $message->{$column}.': '.$message->section->name;
					elseif ($column == 'cat_id')
						echo $message->{$column}.': '.$message->cat->name;
					elseif ($column == 'topic_id')
						echo $message->{$column}.': '.$message->topic->name;
					elseif ($column == 'date')
						echo date('d.m.Y в H:i', $message->{$column});
					elseif ($column == 'open' OR $column == 'deleted')
						echo ($message->{$column} == 1)
							? '<a href="'.URL::site(Route::get("backend")->uri(array(
								'controller' => 'forum',
								'action' => 'message_field_update',
								'par1' => $message->id,
								'par2' => $column,
								'par3' => '0'
							))).'">0</a> | '.intval($message->{$column})
							: intval($message->{$column}).' | <a href="'.URL::site(Route::get("backend")->uri(array(
								'controller' => 'forum',
								'action' => 'message_field_update',
								'par1' => $message->id,
								'par2' => $column,
								'par3' => '1'
							))).'">1</a>';
					else
						echo Text::limit_chars(strip_tags($message->{$column}), 128, '...');
				?></td>
			<?php endforeach; ?>
			<td><a href="<?php echo URL::site(Route::get('backend')->uri(array('controller' => 'forum', 'action' => 'message_edit', 'par1' => $message->id))); ?>">ред.</a></td>
			<td><a href="<?php echo URL::site(Route::get('backend')->uri(array('controller' => 'forum', 'action' => 'message_delete', 'par1' => $message->id))); ?>" onclick="return confirm('Подтверждаете удаление?');">удал.</a></td>
		</tr>
		<?php endforeach; ?>
	</tbody>
</table>

<?php echo $pagination; ?>